<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎注册
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="role">
                            <RadioGroup v-model="form.role">
                                <Radio label="出题人"></Radio>
                                <Radio label="应试者"></Radio>
                            </RadioGroup>
                        </FormItem>

                        <FormItem prop="company">
                            <Select v-model="form.company" filterable multiple placeholder="请选择企业">
                                <Option v-for="item in form.companyList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                            </Select>
                        </FormItem>

                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>注册</Button>
                        </FormItem>
                    </Form>
                    <router-link to="login">立即登陆</router-link>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie'
export default {
  data () {
    return {
      form: {
        userName: 'iview_admin',
        password: '',
        company: '',
        companyList: []
      },
      rules: {
        userName: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]//,
        // company: [
        //   { required: true, message: '必须选择公司', trigger: 'blur' }
        // ]
      }
    }
  },
  methods: {
    handleSubmit () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          Cookies.set('user', this.form.userName)
          Cookies.set('password', this.form.password)
          this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg')
          // if (this.form.userName === 'iview_admin') {
          //   Cookies.set('access', 0)
          // } else {
          //   Cookies.set('access', 1)
          // }
          // this.$router.push({
          //   name: 'home_index'
          // })
          fetch(`http://localhost:8080/api/login`, {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({username: this.form.userName, password: this.form.password})
          }).then(r => r.json())
            .then(json => {
              Cookies.set('username', json.userName)
              Cookies.set('toekn', json.access_token)
              Cookies.set('roles', json.roles)
              this.$router.push({
                name: 'home_index'
              })
            })
            .catch(ex => {
              debugger
              this.$Message.error('登陆失败，用户名或者密码错误')
            })
        }
      })
    }
  }
}
</script>

<style>

</style>
